package com.example.jarapplication;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mylibrary.MyDialog;
import com.example.mylibrary.NetHelper;
import com.example.mylibrary.YuanJiaoImageView;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Map<String, String> mp = new HashMap<>();
//        mp.put("nimu", "قارلار ياغدى لەپ لەپ توشقان كەلدى سەكرەپ تۇتىۋالدىم ئەپلەپ قازانغا سالدىم كەپلەپ");
//        Call call = NetHelper.postSomething("http://192.168.1.10:8080/halo", mp);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                Log.i("ojo", e.getMessage());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                writeSamples(response.body().bytes());
//            }
//        });

        YuanJiaoImageView imageView=findViewById(R.id.yuanjao);
        imageView.setRadius(15);
    }

    public void writeSamples(byte[] samples) {
        File tempMp3;
        try {
            MediaPlayer mediaPlayer = new MediaPlayer();
            tempMp3 = File.createTempFile("result", ".wav");
            tempMp3.deleteOnExit();
            FileOutputStream fos = new FileOutputStream(tempMp3);
            fos.write(samples);
            fos.close();
            mediaPlayer.setDataSource(tempMp3.getPath());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("ojo", e.getMessage());
            e.printStackTrace();
        }
    }
}
